package com.springbootjdbc.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 你咋那么帅
 * @Date 2020/1/20
 */
@Configuration//这个类一加上就报错，不知道咋回事
public class DruidConfig {

    //读取配置文件（application.yml）中以spring.datasource开头的属性以及值，加入到容器中
    @ConfigurationProperties(prefix ="spring.datasource")
    @Bean
    public DataSource druid(){
        return new DruidDataSource();
    }

    //配置druid的监控，需要配置一个管理后台的servlet
    public ServletRegistrationBean staViewServlet(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
        Map<String,String> map=new HashMap<>();
        map.put("loginUsername","root");
        map.put("loginPassword","x13240318126");
        map.put("","");
       bean.setInitParameters(map);
       return bean;
    }

    //配置一个web监控的filter，这个好像用不了，不知道为啥
    public FilterRegistrationBean webStaFilter(){
        FilterRegistrationBean bean=new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
        Map<String,String> map=new HashMap<>();
        map.put("exclusions","*.js,*.css,/druid/*");
        bean.setInitParameters(map);
        //setUrlPatterns方法中放的是一个集合所以需要用到Arrays.asList（）
        bean.setUrlPatterns(Arrays.asList("/*"));
        return bean;
    }

}
